package com.jic.data.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.model.vo.response.HistogramChartResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import java.math.BigDecimal;
import com.jic.data.model.vo.response.SaleStructureBaseDataResponse;
import com.jic.data.model.entity.SaleStructureBaseData;
import com.jic.data.mapper.SaleStructureBaseDataMapper;
import com.jic.data.service.SaleStructureBaseDataService;

import lombok.extern.slf4j.Slf4j;
/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class SaleStructureBaseDataServiceImpl implements SaleStructureBaseDataService {

	@Resource
	private SaleStructureBaseDataMapper mapper;




	/**
	 * 销售结构 销售汇总数据
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public SaleStructureBaseData listAllRecord(SaleStructureBaseData request) {

		return mapper.selectBySelective(request);
	}


	/**
	 * 销售结构：销售来源_会员/非会员 (销售额)
	 * @param request
	 * @return
	 */
    public HistogramChartResponse selectSaleAmount(SaleStructureBaseData request){
		List<SaleStructureBaseData> list = mapper.selectSumOrderAdnAmount(request);

		HistogramChartResponse response = new HistogramChartResponse();
		List<String> xDate = new ArrayList<>();
		List<Double> yMem = new ArrayList<>();
		List<Double> yNonMen = new ArrayList<>();

		list.stream().forEach( category -> {
			xDate.add(DateUtil.format(category.getSaleDate(),"yyyy年MM"));
			yMem.add(category.getMemSaleAmount().doubleValue() / 1000000);
			yNonMen.add(category.getNonMemSaleAmount().doubleValue() / 1000000);
		});
		response.setX(xDate);
		response.setY1(yMem);
		response.setY2(yNonMen);

		return response;
	}

	/**
	 * 销售结构： 订单分布 （每天订单数）
	 * @param request
	 * @return
	 */
    public HistogramChartResponse selectTotalOrder(SaleStructureBaseData request){
		List<SaleStructureBaseData> list = mapper.selectSumOrderAdnAmount(request);

		HistogramChartResponse response = new HistogramChartResponse();
		List<String> xDate = new ArrayList<>();
		List<Double> yMem = new ArrayList<>();
		List<Double> yTotal = new ArrayList<>();

		list.stream().forEach( category -> {
			xDate.add(DateUtil.format(category.getSaleDate(),"yyyy年MM"));
			yMem.add(category.getMemOrderQuantity().doubleValue() / 1000000);
			yTotal.add(category.getTotalOrderQuantity().doubleValue() / 1000000);
		});
		response.setX(xDate);
		response.setY1(yMem);
		response.setY2(yTotal);

		return response;
	}

}
